/*
 * PdfViewer.java
 * 
 * Copyright (c) 2012 Supin P Surendran <supinps@gmail.com>.
 * 
 * This file is part of InventoryMan.
 * 
 * InventoryMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * InventoryMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with InventoryMan.  If not, see <http ://www.gnu.org/licenses/>.
 */

package InventoryMan;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PagePanel;
import java.awt.BorderLayout;
import java.awt.print.PrinterException;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.event.*;
/**
 *
 * @author Supin P Surendran <supinps@gmail.com>
 */
public class PdfViewer implements ActionListener{
    /**
     * PdfViewer can render a pdf file on its window
     * and initiate printing from its window
     * @param f
     */
    public PdfViewer(String f) {

        //set up the frame and panel
        frame = new JFrame("PDF Test");
        frame.setUndecorated(true);
        panel = new PagePanel();
        frame.add(panel);
        tb=new JToolBar();
        left=new JButton();
        right=new JButton();
        print=new JButton();
        cancel=new JButton();
        left.setText("<<");
        left.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        right.setText(">>");
        right.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        print.setText("Print");
        print.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        cancel.setText("Cancel");
        cancel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        left.addActionListener(this);
        right.addActionListener(this);
        print.addActionListener(this);
        cancel.addActionListener(this);
        frame.add(tb,BorderLayout.NORTH);
        tb.add(left);
        tb.add(right);
        tb.add(print);
        tb.add(cancel);
        frame.pack();
        frame.setVisible(true);
        frame.requestFocus();
        //load a pdf from a byte buffer
        File file = new File(f);
        try {
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        pdffile = new PDFFile(buf);
        printPdf=new PdfPrinter(pdffile,"indent");
        pageNo=1;

        // show the first page        
            page = pdffile.getPage(1);
            panel.showPage(page);
        } catch(Exception e) {e.printStackTrace();}

    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==left) {
            if(pageNo>1) {
                pageNo--;
                page=pdffile.getPage(pageNo);
                panel.showPage(page);
            }
        }
        if(e.getSource()==right) {
            if(pageNo<pdffile.getNumPages()) {
                pageNo++;
                page=pdffile.getPage(pageNo);
                panel.showPage(page);
            }
        }
        if(e.getSource()==print) {
            try {
                printPdf.print();
                javax.swing.JOptionPane.showMessageDialog(frame, "Printing Successfull");
                frame.dispose();
            } catch (PrinterException ex) {
                Logger.getLogger(PdfViewer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if(e.getSource()==cancel) {
            frame.dispose();
        }
    }
    PDFFile pdffile;
    PDFPage page;
    PagePanel panel;
    PdfPrinter printPdf;
    int pageNo;
    JFrame frame ;
    JToolBar tb;
    JButton left;
    JButton right;
    JButton print;
    JButton cancel;
}
